// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	/**
	 * getData方法描述
	 * @param {string} params 参数描述
	 * @returns {object} 返回值描述
	 */
	getData(params) {
		// 参数校验，如无参数则不需要
		if (!params) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		const {
			key
		} = params || {}
		try {
			const db = uniCloud.databaseForJQL({
				clientInfo: this.getClientInfo()
			})
			let dbName = ''
			switch (key) {
				case 'phone':
					dbName = 'custom-phone'
					break
				case 'story':
					dbName = 'custom-story'
					break
				case 'notice':
					dbName = 'opendb-notice'
					break
			}
			return db.collection(dbName).orderBy('create_time', 'desc').get({
				getOne: true
			})
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}

	},
	/**
	 * addData方法描述
	 * @param {string} params 参数描述
	 * @returns {object} 返回值描述
	 */
	addData(params) {
		// 参数校验，如无参数则不需要
		if (!params) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		const {
			key,
			data
		} = params || {}
		try {
			const db = uniCloud.databaseForJQL({
				clientInfo: this.getClientInfo()
			})
			let dbName = ''
			switch (key) {
				case 'phone':
					dbName = 'custom-phone'
					break
				case 'story':
					dbName = 'custom-story'
					break
				case 'notice':
					dbName = 'opendb-notice'
					break
			}
			return db.collection(dbName).add(data)
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	}
}